Imports Microsoft.VisualBasic
Imports System.Data

Public Class Secure
    Public ParentPage As Page

    Dim colAddControl As Collection
    Dim colEditControls As Collection
    Dim colDeleteControl As Collection
    Dim colPrintControl As Collection

    Dim colDataGrid As Collection
    Dim colDataGridColumn As Collection
    Dim colDataGridColumnType As Collection

    Public Sub New()
        colAddControl = New Collection
        colEditControls = New Collection
        colDeleteControl = New Collection
        colPrintControl = New Collection

        colDataGrid = New Collection
        colDataGridColumn = New Collection
        colDataGridColumnType = New Collection
    End Sub

    Enum ControlTypes
        AddType = 1
        EditType = 2
        DeleteType = 3
        PrintType = 4
    End Enum

    Public Sub AddControl(ByVal FormControl As Control, ByVal ControlType As ControlTypes)
        Select Case ControlType
            Case ControlTypes.AddType
                colAddControl.Add(FormControl)
            Case ControlTypes.EditType
                colEditControls.Add(FormControl)
            Case ControlTypes.DeleteType
                colDeleteControl.Add(FormControl)
            Case ControlTypes.PrintType
                colPrintControl.Add(FormControl)
        End Select
    End Sub

    Public Sub AddDataGridColumn(ByVal mDataGrid As DataGrid, ByVal Column As Integer, ByVal ControlType As ControlTypes)
        colDataGrid.Add(mDataGrid)
        colDataGridColumn.Add(Column)
        colDataGridColumnType.Add(ControlType)
    End Sub

    Public Sub SetPageAccess(ByVal Username As String)
        Dim dm As New DAOMain.CommonDAO
        Dim i As Integer

        dm.AddParameter(Username)
        dm.AddParameter(Replace(ParentPage.Request.FilePath, "/CIMS", ""))

        Dim dt As DataTable = dm.ExecQryProc("sp_user_access_qry")

        Try
            If Val(dt.Rows(0)("view_tag").ToString) <> -1 Then
                ParentPage.Response.Redirect("~/AccessDenied.aspx")
            End If
            If Val(dt.Rows(0)("add_tag").ToString) <> -1 Then
                For i = 1 To colAddControl.Count
                    colAddControl(i).Visible = False
                Next i

                For i = 1 To colDataGrid.Count
                    If colDataGridColumnType(i) = ControlTypes.AddType Then
                        colDataGrid(i).columns(colDataGridColumn(i)).visible = False
                    End If
                Next
            End If
            If Val(dt.Rows(0)("edit_tag").ToString) <> -1 Then
                For i = 1 To colEditControls.Count
                    colEditControls(i).Visible = False
                Next i

                For i = 1 To colDataGrid.Count
                    If colDataGridColumnType(i) = ControlTypes.EditType Then
                        colDataGrid(i).columns(colDataGridColumn(i)).visible = False
                    End If
                Next
            End If
            If Val(dt.Rows(0)("delete_tag").ToString) <> -1 Then
                For i = 1 To colDeleteControl.Count
                    colDeleteControl(i).Visible = False
                Next i

                For i = 1 To colDataGrid.Count
                    If colDataGridColumnType(i) = ControlTypes.DeleteType Then
                        colDataGrid(i).columns(colDataGridColumn(i)).visible = False
                    End If
                Next
            End If
            If Val(dt.Rows(0)("print_tag").ToString) <> -1 Then
                For i = 1 To colPrintControl.Count
                    colPrintControl(i).Visible = False
                Next i

                For i = 1 To colDataGrid.Count
                    If colDataGridColumnType(i) = ControlTypes.PrintType Then
                        colDataGrid(i).columns(colDataGridColumn(i)).visible = False
                    End If
                Next
            End If
        Catch ex As Exception
            ParentPage.Response.Redirect("~/AccessDenied.aspx")
        End Try
        

        dm = Nothing
    End Sub

    Protected Overrides Sub Finalize()
        colAddControl = Nothing
        colEditControls = Nothing
        colDeleteControl = Nothing
        colPrintControl = Nothing

        MyBase.Finalize()
    End Sub
End Class
